//
//  JingQuHeadView.m
//  ai2ban
//
//  Created by Heartbeats on 2018/3/1.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "JingQuHeadView.h"

@interface JingQuHeadView()
@property (nonatomic,strong)NSMutableArray<UIButton *> *btn1Arrs;
@property (nonatomic,strong)UIView *lineView;
@end

@implementation JingQuHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initTheUI];
    }
    return self;
}

- (void)initTheUI {
    
    self.btn1Arrs = [NSMutableArray array];
    
    self.backgroundColor = [UIColor whiteColor];

    NSArray *titles = @[@"爱吃",@"爱购",@"爱玩",@"爱住"];
    
    CGFloat width = 40;
    CGFloat height = 40;
    CGFloat leftMargin = 12;
    CGFloat margin = (kScreen_Width-leftMargin*2-width*4)/3.0;
    WeakSelf;
    [titles enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat x = leftMargin + idx*(width+margin);
        CGRect frame = CGRectMake(x, 0, width, height);
        UIButton *btn = [[UIButton alloc] init];
        btn.frame = frame;
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:mainColor forState:UIControlStateSelected];
        [btn setTitleColor:COLOR_X(74, 74, 74, 1) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [weakSelf addSubview:btn];
        btn.tag = idx;
        [btn addTarget:self action:@selector(didBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.btn1Arrs addObject:btn];
        
        if (idx == 0) {
            btn.selected = YES;
        }
    }];
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(self.subviews[0].left, 34, self.subviews[1].width, 2);
    lineView.backgroundColor = mainColor;
    [self addSubview:lineView];
    self.lineView = lineView;
}

- (void)didBtnClick:(UIButton *)sender {
    [self.btn1Arrs enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
    }];
    sender.selected = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.x = sender.x;
    }];
    if (self.selectType) {
        self.selectType(sender.tag);
    }
}
@end
